﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Windows.Forms;
using Telerik.WinControls;
using BLL;
using Telerik.WinControls.UI;
using BE;
// omkring tags: da .Tag propertiet i komponenterne er et objekt skal man sørge for det ikke er null, at det er et int og at det findes i tekst biblioteket før det kan blive udskiftet
namespace Language_Editor
{
    //  Summary:
    //      Language manager klassen håndterer oversættelser af en form i runtime ved at udskifte dens tekster
    public class LanguageManager
    {
        private CultureInfo currentLanguage;
        //  Summary
        //      Nuværende sprog programmet befinder sig i
        //      Hvis sproget skiftes til et andet sprog vil languagechanged boolean
        //      blive sat til true og klassen tillader et skift af tekster i runtime
        public CultureInfo CurrentLanguage
        {
            get
            {
                return currentLanguage;
            }
            set
            {
                if (currentLanguage == null || currentLanguage.LCID != value.LCID)
                    LanguageChanged = true;
                currentLanguage = value;
            }
        }
        //under udskiftning af tekster vil den blive fyldt op med tekster indekseret af deres tags
        public Dictionary<int, string> TextLibrary { get; set; }
        private bool LanguageChanged { get; set; }
        
        public LanguageManager(CultureInfo StartingLanguage)
        {
            currentLanguage = StartingLanguage;
            LanguageChanged = false;
        }
        public void changeMyLanguage(Form f)
        {
            if (LanguageChanged)
            {
                Application.CurrentCulture = currentLanguage;
                TextLibrary = languageConnector.getTextsByLanguage(currentLanguage.Name); //fyld tekst dictionarien op
                ChangeText(f); //skift navnet på formtitlen
                ChangeTexts(f);

                if (f.MainMenuStrip != null)
                {
                    MenuStrip strip = f.MainMenuStrip;
                    ChangeMenuItems(strip.Items);

                }
                //ChangeRunTimetexts();
                LanguageChanged = false;
            }
            TextLibrary = null;
        }
        public static List<Control> GetControls(Control form)
        {
            var controlList = new List<Control>();

            foreach (Control childControl in form.Controls)
            {
                // Recurse child controls.
                controlList.AddRange(GetControls(childControl));
                controlList.Add(childControl);
            }
            return controlList;
        }
        private void ChangeTexts(Control c)
        {
            foreach (Control cs in GetControls(c))
            {

                if (isNotDateControl(c))
                    ChangeText(cs);

                if (cs.Controls.Count > 0)//hvis kontrollen indeholder andre kontroller
                {

                    foreach (Control co in cs.Controls)
                    {
                        ChangeTexts(co);
                    }
                }

                if (cs is RadMenu) //check om kontrollen er en af disse typer og derfor skal have speciel behandling fordi teksterne ligger bestemte steder
                {
                    RadMenu menuc = ((RadMenu)cs);
                    if (menuc.Items.Count > 0)
                        ChangeItemsInCollection(menuc.Items.ToArray<RadElement>());
                }
                else if (cs is RadStatusStrip)
                {
                    ChangeItemsInCollection(((RadStatusStrip)cs).Items.ToArray<RadElement>());
                }

                else if (cs is RadGridView)
                {

                    foreach (GridViewColumn col in ((RadGridView)cs).Columns)
                    {

                        ChangeText(col);
                    }
                }
                else if (cs is RadMultiColumnComboBox)
                {
                    foreach (GridViewColumn col in ((RadMultiColumnComboBox)cs).EditorControl.Columns)
                    {
                        ChangeText(col);
                    }
                }
                else if (cs is RadCarousel)
                {
                    RadCarousel car = (RadCarousel)cs;
                    ChangeItemsInCollection(car.Items.ToArray<RadElement>());
                }
                else if (cs is RadWizard)
                {//TEMPORARY DISABLED DUE TO WINDWOS CONTROLS BEING USED
                    /* RadWizard w = (RadWizard)cs;
                     foreach (WizardPage p in w.Pages)
                     {
                         addText(p.Header);
                         addText(p.Title);
                         extractTexts(p.ContentArea);
                     }*/
                }
                else if (cs is RadApplicationMenu)
                {
                    ChangeItemsInCollection(((RadApplicationMenu)cs).ButtonItems.ToArray<RadElement>());
                    ChangeItemsInCollection(((RadApplicationMenu)cs).RightColumnItems.ToArray<RadElement>());
                    ChangeItemsInCollection(((RadApplicationMenu)cs).Items.ToArray<RadElement>());
                }
                else if (cs is RadDropDownButton)
                {
                    ChangeItemsInCollection(((RadDropDownButton)cs).Items.ToArray<RadElement>());
                }

                else if (cs is RadChart)
                {
                    /*
                    RadChart chart = (RadChart)cs;

                    ChangeText(chart);
                    foreach (ChartSeries serie in chart.Series)
                    {

                        ChangeText(serie);
                        if (serie.Items.Count > 0)
                        {
                            foreach (ChartSeriesItem citem in serie.Items)
                            {

                                ChangeText(citem);
                            }
                        }
                    }*/
                }
                else if (cs is RadDropDownList)
                {
                    ChangeComboItems(((RadDropDownList)cs).Items);
                }
                else if (cs is RadListControl)
                {
                    RadListControl list = (RadListControl)cs;
                    ChangeComboItems(list.Items);
                }
                else if (cs is RadPanorama)
                {
                    RadPanorama pano = (RadPanorama)cs;
                    ChangeItemsInCollection(pano.Groups.ToArray<RadElement>());
                    ChangeItemsInCollection(pano.Items.ToArray<RadElement>());
                }
                else if (cs is RadTreeView)
                {

                    ChangeNodes(((RadTreeView)cs).Nodes);
                }
                else if (cs is RadCommandBar)
                {
                    RadCommandBar bar = (RadCommandBar)cs;
                    foreach (CommandBarRowElement row in bar.Rows)
                    {
                        ChangeText(row);
                        foreach (CommandBarStripElement strip in row.Strips)
                        {

                            ChangeText(strip);
                            ChangeItemsInCollection(strip.Items.ToArray<RadElement>());
                        }
                    }
                }
                else if (cs is RadRibbonBar)
                {
                    RadRibbonBar ribbon = (RadRibbonBar)cs;
                    ChangeItemsInCollection(ribbon.StartMenuItems.ToArray<RadElement>());
                    ChangeItemsInCollection(ribbon.StartMenuRightColumnItems.ToArray<RadElement>());
                    ChangeItemsInCollection(ribbon.StartMenuBottomStrip.ToArray<RadElement>());
                    ChangeItemsInCollection(ribbon.QuickAccessToolBarItems.ToArray<RadElement>());
                    foreach (ContextualTabGroup group in ribbon.ContextualTabGroups)
                    {

                        ChangeText(group);
                        foreach (RibbonTab tab in group.TabItems)
                        {

                            ChangeText(tab);
                            ChangeItemsInCollection(tab.Items.ToArray<RadElement>());
                        }
                    }
                }
                else if (cs is RadRotator)
                {
                    RadRotator rotator = (RadRotator)cs;
                    ChangeItemsInCollection(rotator.Items.ToArray<RadElement>());
                }
                else if (cs is RadListView)
                {
                    RadListView listView = (RadListView)cs;
                    if (listView.Items.Count > 0)
                    {
                        
                            foreach (ListViewDataItem item in listView.Items)
                            {

                                if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                                    item.Text = TextLibrary[(int)item.Tag];
                            }
                            
                    }
                    if (listView.Groups.Count > 0)
                    {
                            foreach (ListViewDataItemGroup item in listView.Groups)
                            {
                                if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                                    item.Text = TextLibrary[(int)item.Tag];
                            }
                    }
                }
                #region win controls

                //#################################Windows controls#########################
                else if (cs is TreeView)
                {
                    ChangeNodes(((TreeView)cs).Nodes);
                }
                /*else if (cs is DataGridView) //windows gridview
                {
                    DataGridView grid = (DataGridView)cs;
                    foreach (DataGridViewColumn col in grid.Columns)
                    {
                        addText(col.HeaderText);
                    }
                }*/
                else if (cs is ToolStrip)
                {
                    ChangeMenuItems(((ToolStrip)cs).Items);
                }
                    //@StringPlaceHolder#
                    //da det ikke er muligt at gemme indhold i diverse listbokse og kombobokse lavet af mikrosoft
                    //er det muligt at fylde den med disse stringplaceholders programmatisk og de objekter
                    //der er af typen stringplaceholder kan derfor blive ændret fordi de har information om hvad for et tag de har
                else if (cs is ComboBox)
                {
                    ComboBox combobox = (ComboBox)cs;
                    object[] olist = new object[combobox.Items.Count];
                    for (int i = 0; i < combobox.Items.Count; i++)
                    {
                        if (combobox.Items[i] is StringPlaceHolder)
                        {
                            StringPlaceHolder item = (StringPlaceHolder)combobox.Items[i];
                            if (item.Tag > 0 && TextLibrary.ContainsKey(item.Tag))
                                item.Text = TextLibrary[item.Tag];
                        }
                        olist[i] = combobox.Items[i];
                    }
                    combobox.Items.Clear();
                    foreach (object o in olist)
                    {
                        combobox.Items.Add(o);
                    }
                }
                else if (cs is ListBox)
                {//se @StringPlaceHolder#
                    ListBox listBox = (ListBox)cs;
                    object[] olist = new object[listBox.Items.Count];
                    for (int i = 0; i < listBox.Items.Count; i++)
                    {
                        if (listBox.Items[i] is StringPlaceHolder)
                        {
                            StringPlaceHolder item = (StringPlaceHolder)listBox.Items[i];
                            if (item.Tag > 0 && TextLibrary.ContainsKey(item.Tag))
                                item.Text = TextLibrary[item.Tag];

                        }
                        olist[i] = listBox.Items[i];
                    }
                    listBox.Items.Clear();
                    foreach (object o in olist)
                    {
                        listBox.Items.Add(o);
                    }

                }
                else if (cs is ListView)
                {
                    ListView listView = (ListView)cs;
                    if (listView.Items.Count > 0)
                    {


                        foreach (ListViewItem item in listView.Items)
                        {
                            if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                                item.Text = TextLibrary[(int)item.Tag];
                            /* if (item.SubItems.Count > 0) //hvis microsoft laver deres listviewsubitems om så de kommer med i designeren
                             {
                             foreach (ListViewItem.ListViewSubItem sub in item.SubItems)
                             {
                                     
                             }*/
                        }
                    }
                    if (listView.Groups.Count > 0)
                    {
                        foreach (ListViewGroup item in listView.Groups)
                        {
                            if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                                item.Header = TextLibrary[(int)item.Tag];
                        }
                    }
                    if (listView.Columns.Count > 0)
                    {
                        foreach (ColumnHeader item in listView.Columns)
                        {
                            if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                                item.Text = TextLibrary[(int)item.Tag];
                        }
                    }
                }
                #endregion
            }

        }
        public string getText(string initialText, int tag) //finder teksten til et tag, sproget bliver fundet fra det nuværende sprog i klassen, hvis teksten ikke findes i databasen bliver initial tekst returneret som er dens originale tekst
        {
            string text = languageConnector.getText(tag, currentLanguage.Name);
            if (text == null || text.Length < 1)
                return initialText;
            return text;
        }

        private void ChangeText(GridViewColumn col) //skift teksten i en kolonne i en radgridview
        {
            if (col.Name.Contains('$'))
            {
                string[] values = col.Name.Split('$');
                int tag = Convert.ToInt32(values[1]);
                if (TextLibrary.ContainsKey(tag))
                    col.HeaderText = TextLibrary[tag];
            }

        }
        private void ChangeNodes(RadTreeNodeCollection nodes)
        {
            foreach (RadTreeNode node in nodes)
            {
                if (node.Nodes.Count > 0)
                    ChangeNodes(node.Nodes);
                if (node.Tag != null && TextLibrary.ContainsKey((int)node.Tag))
                    node.Text = TextLibrary[(int)node.Tag];
            }

        }
        private void ChangeNodes(TreeNodeCollection nodes)
        {
            foreach (TreeNode node in nodes)
            {
                if (node.Nodes.Count > 0)
                    ChangeNodes(node.Nodes);
                if (node.Tag != null && TextLibrary.ContainsKey((int)node.Tag))
                    node.Text = TextLibrary[(int)node.Tag];
            }
        }
        private void ChangeItemsInCollection(RadElement[] colle)
        {
            foreach (RadItem im in colle)
            {
                ChangeText(im);
                if (hasItems(im))
                {
                    ChangeItemsInCollection(im.Children.ToArray<RadElement>());
                }
            }
        }

        //check om raditemet har items i dens kollektion, nogle extendede klasser af 
        //raditem indeholder items på en anden måde og skal derfor behandles anderledes

        public bool hasItems(RadItem item)
        {
            if (item is RadItemsContainer)
                return item.Children.Count > 0;
            else if (item is RadRotatorElement)
            {
                ChangeItemsInCollection(((RadRotatorElement)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is RadMenuComboItem)
            {
                ChangeComboItems(((RadMenuComboItem)item).ComboBoxElement.Items);
                return false;
            }
            else if (item is RadDropDownListElement)
            {
                ChangeComboItems(((RadDropDownListElement)item).Items);
                return false;
            }
            else if (item is CommandBarDropDownButton)
            {
                ChangeItemsInCollection(((CommandBarDropDownButton)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is CommandBarDropDownList)
            {
                ChangeComboItems(((CommandBarDropDownList)item).Items);
                return false;
            }
            else if (item is RadMenuButtonItem)
            {
                RadMenuButtonItem but = (RadMenuButtonItem)item;
                ChangeItemsInCollection(but.Items.ToArray<RadElement>());
                return false;
            }
            else if (item is RadMenuItemBase)
            {
                ChangeItemsInCollection(((RadMenuItemBase)item).Items.ToArray<RadElement>());
                return false;
            }
            else if (item is TileGroupElement)
            {
                ChangeItemsInCollection(((TileGroupElement)item).Items.ToArray<RadElement>());
                return false;
            }



            return false;
        }

        private void ChangeComboItems(RadListDataItemCollection items)
        {

            foreach (RadListDataItem item in items)
            {
                if (TextLibrary.ContainsKey((int)item.Tag))
                {
                    item.Text = TextLibrary[(int)item.Tag];
                }
            }



        }

        private void ChangeMenuItems(ToolStripItemCollection toolStripItemCollection)
        {

            //windows toolstrips
            foreach (ToolStripItem item in toolStripItemCollection)
            {
                if (item.Tag != null && item.Tag is int && TextLibrary.ContainsKey((int)item.Tag))
                    item.Text = TextLibrary[(int)item.Tag];
                if (item is ToolStripComboBox)//toolstripcombobox and should not enter recursive
                {
                    ToolStripComboBox combobox = (ToolStripComboBox)item;
                    if (combobox.Items.Count > 0)
                    {

                        object[] olist = new object[combobox.Items.Count];
                        for (int i = 0; i < combobox.Items.Count; i++)
                        {// se @StringPlaceHolder#
                            if (combobox.Items[i] is StringPlaceHolder)
                            {
                                StringPlaceHolder sitem = (StringPlaceHolder)combobox.Items[i];
                                if (sitem.Tag > 0 && TextLibrary.ContainsKey(sitem.Tag))
                                    sitem.Text = TextLibrary[sitem.Tag];

                            }
                            olist[i] = combobox.Items[i];
                        }
                        combobox.Items.Clear();
                        foreach (object o in olist)
                        {
                            combobox.Items.Add(o);
                        }

                    }
                }
                else if (item is ToolStripDropDownItem) //dropdown item and should enter recursive
                {
                    ToolStripDropDownItem v = (ToolStripDropDownItem)item;
                    if (v.DropDownItems.Count > 0)
                    {

                        ChangeMenuItems(v.DropDownItems);

                    }
                }
            }
        }
        private void ChangeText(Control cs)
        {
            if (cs.Tag != null && cs.Tag is int && TextLibrary.ContainsKey((int)cs.Tag))
                cs.Text = TextLibrary[(int)cs.Tag];
        }
        private void ChangeText(RadItem cs)
        {
            if (cs.Tag != null && cs.Tag is int && TextLibrary.ContainsKey((int)cs.Tag))
                cs.Text = TextLibrary[(int)cs.Tag];
        }
        private bool isNotDateControl(Control c)
        {

            return !(c is RadSpinEditor || c is DateTimePicker || c is MonthCalendar || c is RadTimePickerElement || c is RadTimePicker || c is RadDateTimeEditor || c is RadDateTimePicker || c is RadDateTimeEditorElement || c is RadDateTimePickerCalendar);

        }
    }
}
